package com.mayikt.portal.controller;

import cn.hutool.core.util.StrUtil;
import com.mayikt.base.BaseResponse;
import com.mayikt.base.BaseWebController;
import com.mayikt.feign.MemberServiceFeign;
import com.mayikt.member.output.dto.UserOutDTO;
import com.mayikt.web.constants.Constants;
import com.mayikt.web.util.CookieUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class IndexController extends BaseWebController {
    private static final String INDEX_FTL = "index";
    @Autowired
    private MemberServiceFeign memberServiceFeign;
    /**
     * 跳转index页面
     * @return
     */
    @RequestMapping("/")
    public String index(HttpServletRequest request, Model model){
        String token = CookieUtils.getCookieValue(request, Constants.LOGIN_TOKEN_COOKIENAME, true);
        if(!StrUtil.isEmpty(token)){
            BaseResponse<UserOutDTO> result = memberServiceFeign.getInfo(token);
            if(com.mayikt.constants.Constants.HTTP_RES_CODE_200.equals(result.getCode())){
                String mobile = result.getData().getMobile();
//                电话号码脱敏操作
                String desensMobile = mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
                model.addAttribute("desensMobile", desensMobile);
            }
        }
        return INDEX_FTL;
    }
}
